<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <%@ page import ="java.util.List"%>


<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
request.setCharacterEncoding("utf-8");
//request.setAttribute("splace", request.getParameter("splace"));
//request.setAttribute("eplace", request.getParameter("eplace"));

%> 
    
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath %>">
    <meta charset="UTF-8">
    <title>Basic CRUD Application - jQuery EasyUI CRUD Demo</title>
  <link rel="stylesheet" type="text/css" href="js/util/easyui/themes/default/easyui.css">
	<link rel="stylesheet" type="text/css" href="js/util/easyui/themes/icon.css">
	
	<script type="text/javascript" src="js/util/jquery/jquery-3.2.1.min.js"></script>
	<script type="text/javascript" src="js/util/easyui/jquery.easyui.min.js"></script>

</head>
<body>
    <h2>个人信息</h2>
   
    
    <table id="dg" title="My Users" class="easyui-datagrid" style="width:700px;height:250px"
            url="UserInfoServlet" method="get"
            toolbar="#toolbar" pagination="true"
            rownumbers="true" fitColumns="true" singleSelect="true">
        <thead>
            <tr>
                <th field="userid" width="30">用户id</th>
                <th field="username" width="30">用户名</th>
                <th field="sex" width="50">性别</th>
                <th field="province" width="50">省份</th>
                <th field="city" width="50">市县</th>
                <th field="cardid" width="55">身份证号码</th>
                <th field="phonenum" width="50">电话号码</th>
            </tr>
        </thead>
    </table>
    <div id="toolbar">
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newUser()">New User</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editUser()">Edit User</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="destroyUser()">Remove User</a>
    </div>
    
    <div id="dlg" class="easyui-dialog" style="width:400px"
            closed="true" buttons="#dlg-buttons">
        <form id="fm" method="post" novalidate style="margin:0;padding:20px 50px">
            <div style="margin-bottom:20px;font-size:14px;border-bottom:1px solid #ccc">User Information</div>
            <div style="margin-bottom:10px">
                <input id="username" name="username" class="easyui-textbox" required="true" label="用户名:" style="width:100%">
            </div>
           
            <div style="margin-bottom:10px">
                <input id="phonenum" name="phonenum" class="easyui-textbox" required="true" label="电话号码:" style="width:100%">
            </div>
            
        </form>
    </div>
    <div id="dlg-buttons">
        <a href="javascript:void(0)" class="easyui-linkbutton c6" iconCls="icon-ok" onclick="saveUser()" style="width:90px">Save</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')" style="width:90px">Cancel</a>
    </div>
    <script type="text/javascript">
        var url;
        function newUser(){
            $('#dlg').dialog('open').dialog('center').dialog('setTitle','New User');
            $('#fm').form('clear');
            url = 'save_user.php';
        }
        function editUser(){
            var row = $('#dg').datagrid('getSelected');
            if (row){
                $('#dlg').dialog('open').dialog('center').dialog('setTitle','Edit User');
                $('#fm').form('load',row);
                url = 'update_user.php?id='+row.id;
            }
        }
        function saveUser(){
        	
            var row = $('#dg').datagrid('getSelected');
            var username=document.getElementById("username").value;
            var phonenum=document.getElementById("phonenum").value;
            
            alert(row.userid);
          	 $.ajax({
              	 url:'UserEditServlet',
              	 cache:false,
              	 dataType:"JSON",
              	 type:"POST",
              	 data:{
              		 
              		 "userid":row.userid,     
              		 "username":username,
              		 "phonenum":phonenum,
              		  
              	 },
              	success:function(result){
           		 /* Dialog.alert("提示：你点击了一个按钮");*/
           		 alert("修改成功");
           		 $('#dlg').dialog('close');   
           		 $('#dg').datagrid("reload",{});
           	 }
            });
       }

        function destroyUser(){
            var row = $('#dg').datagrid('getSelected');
            if (row){
                $.messager.confirm('Confirm','Are you sure you want to destroy this user?',function(r){
                    if (r){
                        $.post('destroy_user.php',{id:row.id},function(result){
                            if (result.success){
                                $('#dg').datagrid('reload');    // reload the user data
                            } else {
                                $.messager.show({    // show error message
                                    title: 'Error',
                                    msg: result.errorMsg
                                });
                            }
                        },'json');
                    }
                });
            }
        }
    </script>
</body>
</html>